package com.longrise.android.byjk.plugins.hra.hratabsecond;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.plugins.hra.HraSearchActivity;
import com.longrise.android.byjk.plugins.hra.SearchBoxView;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.PrintLog;

import static com.longrise.android.byjk.R.id.search_toolbar;

/**
 * Created by Administrator on 2018/1/12.
 *我的预约--搜索结果页面
 */

public class OrderSearchActivity extends BaseActivity2<MineAppointPresenter> implements MineAppointContract.View, View.OnClickListener, TextWatcher, View.OnFocusChangeListener {

    public static final String TAG = "OrderSearchActivity";
    private TabLayout mTb;
    private ViewPager mVp;
    private String searchText;
    private SearchBoxView mSearchToolbar;
    private ImageView iv_order_search_back;
    private TextView tv_hra_search;
    private LinearLayout order_search_hint;
    private TextView tv_hra_search_result;
    private LinearLayout ll_search;
    private RelativeLayout rl_order_search_tab;
    private EntityBean[] mHraOrderListBean;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_order_search;
    }

    @Override
    public void initView() {
        mTb = (TabLayout) findViewById(R.id.order_search_tablayout);
        mVp = (ViewPager) findViewById(R.id.order_search_viewpager);
        mSearchToolbar = (SearchBoxView) findViewById(search_toolbar);
        iv_order_search_back = (ImageView) findViewById(R.id.iv_order_search_back);  //返回按钮
        tv_hra_search = (TextView) findViewById(R.id.tv_hra_search);  //搜索按钮
        order_search_hint = (LinearLayout) findViewById(R.id.order_search_hint);
        tv_hra_search_result = (TextView) findViewById(R.id.tv_hra_search_result);
        ll_search = (LinearLayout) findViewById(R.id.ll_search);
        rl_order_search_tab = (RelativeLayout) findViewById(R.id.rl_order_search_tab);
        initAdapter();
        setToolbarVisible(false);
        getExtra();
        if(mSearchToolbar.getEditText().isFocusable()){
            hideSoftKey(mSearchToolbar.getEditText());
            PrintLog.e(TAG,"mSearchToolbar"+mSearchToolbar.getEditText().isFocusable());
        }
        initEvent();
    }

    public void getExtra(){
        Intent intent = getIntent();
        searchText = intent.getStringExtra(HraSearchActivity.HRASEARCH);
        Control.setOrderSearchName(searchText);
        mSearchToolbar.setText(Control.getOrderSearchName());
        mSearchToolbar.setRightClickMode(SearchBoxView.USERIF);
    }

    public void initAdapter(){
        OrderSearchPagerAdapter pagerAdapter = new OrderSearchPagerAdapter(getSupportFragmentManager());
        mVp.setAdapter(pagerAdapter);
        mTb.setupWithViewPager(mVp);
    }

    public void initEvent(){
        iv_order_search_back.setOnClickListener(this);
        tv_hra_search.setOnClickListener(this);
//        mSearchToolbar.getEditText().setFocusable(false);//让EditText失去焦点，然后获取点击事件
        mSearchToolbar.getEditText().setOnFocusChangeListener(this);
        mSearchToolbar.getEditText().addTextChangedListener(this);
        order_search_hint.setOnClickListener(this);
    }


    @Override
    public void onToolbarBackClick() {

    }

    public void hideSoftKey(EditText editText){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0) ;
            PrintLog.e(TAG,"11111111111111");
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击提示框
            case R.id.order_search_hint:
                rl_order_search_tab.setVisibility(View.VISIBLE);
                mVp.setVisibility(View.VISIBLE);
                order_search_hint.setVisibility(View.GONE);
                if(mSearchToolbar.getEditText().isFocusable()){
                    mSearchToolbar.getEditText().clearFocus();
                    mSearchToolbar.getEditText().setFocusableInTouchMode(true);
                    hideSoftKey(mSearchToolbar.getEditText());
                    PrintLog.e(TAG,"mSearchToolbar"+mSearchToolbar.getEditText().isFocusable());
                }
                initAdapter();
                break;
            //点击返回按钮
            case R.id.iv_order_search_back:
                finish();
                break;
            //点击搜索按钮
            case R.id.tv_hra_search:
                rl_order_search_tab.setVisibility(View.VISIBLE);
                mVp.setVisibility(View.VISIBLE);
                order_search_hint.setVisibility(View.GONE);
                if(mSearchToolbar.getEditText().isFocusable()){
                    mSearchToolbar.getEditText().clearFocus();
                    mSearchToolbar.getEditText().setFocusableInTouchMode(true);
                    hideSoftKey(mSearchToolbar.getEditText());
                    PrintLog.e(TAG,"mSearchToolbar"+mSearchToolbar.getEditText().isFocusable());
                }

                if(!TextUtils.isEmpty(mSearchToolbar.getEditText().getText().toString())){
                    initAdapter();
                }
                break;

        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        String text = mSearchToolbar.getText().toString().trim();
        if (!TextUtils.isEmpty(text)) {
            tv_hra_search.setEnabled(true);
            Control.setOrderSearchName(s.toString());
            order_search_hint.setVisibility(View.VISIBLE);
            tv_hra_search_result.setText(Html.fromHtml("搜索 " + "&#34"+s + "&#34" + " 的HRA预约"));
        } else {
            tv_hra_search.setEnabled(false);
            order_search_hint.setVisibility(View.GONE);
        }

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        String username = mSearchToolbar.getText();
        if(!hasFocus){ //失去焦点
            mSearchToolbar.setRightIcVisible(false);
            rl_order_search_tab.setVisibility(View.VISIBLE);
            mVp.setVisibility(View.VISIBLE);
            order_search_hint.setVisibility(View.GONE);
        }else{
            if(!TextUtils.isEmpty(username)) {
                rl_order_search_tab.setVisibility(View.GONE);
                mVp.setVisibility(View.GONE);
                order_search_hint.setVisibility(View.VISIBLE);
                mSearchToolbar.setRightIcVisible(true);
                Control.setOrderSearchName(username);
                tv_hra_search_result.setText(Html.fromHtml("搜索 " + "&#34"+ username + "&#34" + " 的HRA预约"));
            }
        }
    }

    @Override
    public void refreshData(EntityBean bean) {

    }

    @Override
    public void setRefreshing(boolean isRefreshing) {

    }
}
